//
//  FirstViewControllerOfFamous.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/30.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "FirstViewControllerOfFamous.h"
#import "HWLNetworkTool.h"
#import "HotModel.h"
#import "Hot_image_Model.h"
#import "Hot_New_Model.h"
#import "Hot_ViewPoint_Model.h"
#import "UIImageView+WebCache.h"
#import "HotImageVewCell.h"
#import "ViewPointView.h"
#import "MJRefresh.h"
#import "ViewPointTableviewController.h"
#import "ProductDetailTableViewController.h"


#import "HotNewProductCell.h"
@interface FirstViewControllerOfFamous ()
@property (nonatomic, retain) HotModel *hotmodel;
@property (nonatomic, retain) Hot_image_Model *model;

@end

@implementation FirstViewControllerOfFamous



-(void)viewWillAppear:(BOOL)animated{

 }


- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[HotNewProductCell class] forCellReuseIdentifier:@"HotNewProductCell"];
    [self.tableView registerClass:[HotImageVewCell class] forCellReuseIdentifier:@"HotImageVewCell"];
    [self.tableView registerClass:[ViewPointView class] forCellReuseIdentifier:@"ViewPointViewCell"];
    [self handleData];
    
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf handleData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        [weakSelf handleData];
        
    }];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(execute:)
                                                 name:@"indexNumber"
                                               object:nil];
}




- (void)execute:(NSNotification *)noti{

    NSString *str = [noti object];
    NSInteger a = [str integerValue];
    NSString *picid = self.hotmodel.top_big_photo_cigarettes[a].uid;
    ViewPointTableviewController *point = [[ViewPointTableviewController alloc]init];
    point.viewId = picid;
    [self.navigationController pushViewController:point animated:YES];
    

}

- (void)handleData{
   
    
    NSString *dicOfBody = [@"openudid"stringByAppendingString:@"OPENUDID"];
   // NSDictionary *dicOfBody = @{@"openudid":@"OPENUDID"};
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
   [HWLNetworkTool POST:@"http://app.yanyue.cn/index/hot" andBody:dicOfBody andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        self.hotmodel = [HotModel modelWithDic:result];
       [self.tableView reloadData];
       
       [self.tableView.mj_header endRefreshing];
       [self.tableView.mj_footer endRefreshing];
    
       
    } andFailureBlock:^(NSError *error) {}];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSMutableArray *arrayOfTitle = [NSMutableArray array];
        NSMutableArray *arrayOfImageUrl = [NSMutableArray array];
        HotImageVewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HotImageVewCell"];
        
            for (int i = 0 ; i < self.hotmodel.top_big_photo_cigarettes.count; i++) {
                
                [arrayOfTitle addObject: self.hotmodel.top_big_photo_cigarettes[i].title];
                [arrayOfImageUrl addObject:self.hotmodel.top_big_photo_cigarettes[i].photo];
   
        }
        cell.titles = arrayOfTitle;
        cell.imagesURLStrings = arrayOfImageUrl;
        cell.tag = 10000;
        return cell;
    }
    
    
    if (indexPath.section == 1) {
       
        HotNewProductCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HotNewProductCell"];
        cell.name.text = self.hotmodel.nnew_arrival_cigarettes[indexPath.row].name;
        NSURL *url = [NSURL URLWithString:self.hotmodel.nnew_arrival_cigarettes[indexPath.row].photo];
       [cell.imageViewLift sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.catrameinput.text = self.hotmodel.nnew_arrival_cigarettes[indexPath.row].catrame;
        cell.priceinput.text = self.hotmodel.nnew_arrival_cigarettes[indexPath.row].price;
        
        return cell;
    }
    
    if (indexPath.section == 2) {
        
        ViewPointView *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ViewPointViewCell"];
        cell.title.text = self.hotmodel.hot_viewpoints[indexPath.row].title;
        cell.content.text = self.hotmodel.hot_viewpoints[indexPath.row].content;
        NSURL *url = [NSURL URLWithString:self.hotmodel.hot_viewpoints[indexPath.row].photo];
        [cell.imageViewRight sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
      
        return cell;
    }
   
        return nil;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        
        return nil;
        
    }else if(section == 1){
    
    return @"新品上市";
        
    }else if (section == 2){
    
    return @"最近视点";
        
    }
    
    return nil;


}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        
        return 1;
    }
    
    if (section == 1) {
        
        return self.hotmodel.nnew_arrival_cigarettes.count;
    }
    
    
    if (section == 2) {
        return self.hotmodel.hot_viewpoints.count;
    }
    
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 200;
    }
    
    else if (indexPath.section == 1) {
        
        return 95;
        
    }else if (indexPath.section == 2)
    {
        return 95;
    
    }

    return 95;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (indexPath.section == 1) {
        
        ProductDetailTableViewController *productDetail = [[ProductDetailTableViewController alloc]init];
        productDetail.productId = self.hotmodel.nnew_arrival_cigarettes[indexPath.row].uid;
        
        [self.navigationController pushViewController:productDetail animated:YES];
        
    }
 
    if (indexPath.section == 2) {
        
        
        NSString *picid = self.hotmodel.hot_viewpoints[indexPath.row].uid;
        ViewPointTableviewController *point = [[ViewPointTableviewController alloc]initWithStyle:UITableViewStylePlain];
        point.viewId = picid;
        [self.navigationController pushViewController:point animated:YES];

    }


}

-(void)dealloc {


    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"indexNumber"
                                               object:nil];


}



@end
